package domain;

import enums.ResultCode;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;

@Data
@AllArgsConstructor
@NoArgsConstructor
public class R <T>{

    // 状态码
    private int code;
    // 提示信息
    private String msg;
    // 响应数据
    private T data;

    //基础方法
    public static <T> R<T> baseMethod(T data, ResultCode resultCode) {
        R<T> r = new R<>();
        r.setCode(resultCode.getCode());
        r.setMsg(resultCode.getMsg());
        r.setData(data);
        return r;
    }

    //成功方法，无参数
    public static <T> R<T> success() {
        return baseMethod(null, ResultCode.SUCCESS);
    }

    //成功方法，有参数
    public static <T> R<T> success(T data) {
        return baseMethod(data, ResultCode.SUCCESS);
    }

    public static <T> R<T> success(ResultCode resultCode) {
        return baseMethod(null, resultCode);
    }

    public static <T> R<T> success(T data,ResultCode resultCode) {
        return baseMethod(data, resultCode);
    }



    //失败方法，无参数
    public static <T> R<T> failed() {
        return baseMethod(null, ResultCode.FAILED);
    }

    public static <T> R<T> failed(ResultCode resultCode) {
        return baseMethod(null, resultCode);
    }

    /**
     * 失败方法，有参数
     * @param data
     * @param resultCode
     * @return
     * @param <T>
     */
    public static <T> R<T> failed(T data,ResultCode resultCode) {
        return baseMethod(data, resultCode);
    }


}
